package com.huluobo.jetpackkotlindemo2006a.hilt

import com.squareup.okhttp.Call
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * Created by lc on 2023/2/17.
 * 1.@Module 表示这个类适用于提供依赖注入的实例的模块
 * 2.@InstallIn(ApplicationComponent::class) 表示要装在到哪个模块中
 * 3.ApplicationComponent 表示装载到Activity
 * 4.@Provides 表示当前修饰的方法的返回类型被暴露出去.在外部可以通过返回类型直接使用该方法
 */
@Module
@InstallIn(ApplicationComponent::class)
class NetUtils {
//    @Config.OkHttpClientCall
//    @Provides
//    fun getOkHttpClientCall(url: String): Call {
//        val okHttpClient = OkHttpClient()
//        okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS)
//        val request = Request.Builder().url(url).build()
//        return okHttpClient.newCall(request)
//    }

//    @Config.OtherOkHttpClientCall
//    @Provides
//    fun getOtherOkHttpClientCall(url: String): Call {
//        val okHttpClient = OkHttpClient()
//        okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS)
//        val request = Request.Builder().url(url).build()
//        return okHttpClient.newCall(request)
//    }

    @Provides
    fun getRetrofit(url: String): Retrofit {
        return Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build()
    }
}